package com.future.hedongjun.mall;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.future.baselib.adapter.FragmentAdapter;
import com.future.baselib.entity.BaseResponse;
import com.future.baselib.entity.DefaultResponse;
import com.future.baselib.http.HttpRequest;
import com.future.hedongjun.R;
import com.future.hedongjun.common.MyApp;
import com.future.hedongjun.mall.activity.MallListFragment;
import com.future.hedongjun.mall.activity.ShoppingCarActivity;
import com.future.hedongjun.mall.bean.MallType;
import com.future.hedongjun.mall.bean.MallTypeResponse;
import com.future.hedongjun.mine.ui.LoginActivity;
import com.jaeger.library.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @author jniu
 * @Email niuxiaoqi@hztianque.com
 * @time 2018/11/10
 */
public class MallFragment extends Fragment implements OnTabSelectListener, ViewPager.OnPageChangeListener {

    @BindView(R.id.iv_left)
    ImageView ivLeft;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.view_line)
    View viewLine;
    @BindView(R.id.layout_title)
    FrameLayout layoutTitle;
    @BindView(R.id.iv_shopping_car)
    ImageView ivShoppingCar;
    @BindView(R.id.tab_layout)
    SlidingTabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.status_bar)
    View statusBar;
    Unbinder unbinder;
    private FragmentAdapter adapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mall, container, false);
        unbinder = ButterKnife.bind(this, view);
        statusBar.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(getContext())));
        ivLeft.setVisibility(View.GONE);
        tvTitle.setText("商城");

        new HttpRequest<MallTypeResponse>()
                .with(getContext())
                .setPath("/addons/litestore/api.category/lists")
                .addSuccessListener(new HttpRequest.OnNetworkSuccess() {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        List<MallType> data = ((MallTypeResponse) response).data;
                        MallType type = new MallType();
                        type.setId("");
                        type.setName("全部");
                        data.add(0, type);
                        List<Fragment> fragments = new ArrayList<>();
                        ArrayList<String> titles = new ArrayList<>();
                        for (int i = 0; i < data.size(); i++) {
                            fragments.add(MallListFragment.getInstance(data.get(i).id));
                            titles.add(data.get(i).name);
                        }
                        adapter = new FragmentAdapter(getChildFragmentManager(), fragments);
                        adapter.setTitles(titles);
                        viewPager.setAdapter(adapter);
                        viewPager.addOnPageChangeListener(MallFragment.this);
                        tabLayout.setViewPager(viewPager);
                    }
                }).start(new MallTypeResponse());
        return view;
    }

    /**
     * 获取状态栏高度
     *
     * @param context context
     * @return 状态栏高度
     */
    private static int getStatusBarHeight(Context context) {
        // 获得状态栏高度
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        return context.getResources().getDimensionPixelSize(resourceId);
    }

    private String[] initTestTab() {
        String[] titles = new String[10];
        for (int i = 0; i < 10; i++) {
            titles[i] = "分类" + i;
        }
        return titles;
    }

    private List<Fragment> initFragments() {
        List<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            fragments.add(new MallListFragment());
        }
        return fragments;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.iv_shopping_car)
    public void onViewClicked() {
        if (MyApp.getInstance().hasLogin()) {
            startActivity(new Intent(getContext(), ShoppingCarActivity.class));
        } else {
            startActivity(new Intent(getContext(), LoginActivity.class));
        }

    }

    @Override
    public void onTabSelect(int position) {
        viewPager.setCurrentItem(position);
    }

    @Override
    public void onTabReselect(int position) {

    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {

    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }
}
